//
//  TBRAddressBookListC.m
//  TBRentClient
//
//  Created by tonbright on 2018/5/2.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRAddressBookListC.h"
#import <PPGetAddressBook.h>
#import "TBRAddressBookListCell.h"
@interface TBRAddressBookListC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, copy) NSDictionary *contactPeopleDict;
@property (nonatomic, copy) NSArray *keys;
@end

@implementation TBRAddressBookListC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    //加载数据
    [self loadData];
}

- (void)setupUI {
    //设置导航条
    [self setupNavItem];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRAddressBookListCell" bundle:nil] forCellReuseIdentifier:@"TBRAddressBookListCellId"];
    self.tableView.rowHeight = 52;
}

- (void)setupNavItem {
    self.titleText = @"联系人";
}

#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _keys.count;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *key = _keys[section];
    return [_contactPeopleDict[key] count];
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    TBRAddressBookListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TBRAddressBookListCellId" forIndexPath:indexPath];
    NSString *key = _keys[indexPath.section];
    PPPersonModel *people = [_contactPeopleDict[key] objectAtIndex:indexPath.row];
    
    cell.imv_icon.image = people.headerImage ? people.headerImage : [UIImage imageNamed:@"defultPersonIcon"];
    cell.lbe_name.text = people.name;
    cell.imv_icon.layer.cornerRadius = 20;
    cell.imv_icon.clipsToBounds = YES;
    return cell;
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return _keys[section];
}

//右侧的索引
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return _keys;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *key = _keys[indexPath.section];
    PPPersonModel *people = [_contactPeopleDict[key] objectAtIndex:indexPath.row];
    
    //取出姓名-------
    NSString *name = people.name;
    NSString *phoneNum = people.mobileArray.firstObject;
}



// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {
//    [self progressHUDShowWithText:@"加载数据中..."];
     __weak typeof(self) weakSelf = self;
    //获取按联系人姓名首字拼音A~Z排序(已经对姓名的第二个字做了处理)
    [PPGetAddressBook getOrderAddressBook:^(NSDictionary<NSString *,NSArray *> *addressBookDict, NSArray *nameKeys) {
//        [weakSelf progressHUDHideImmediately];
        //装着所有联系人的字典
        weakSelf.contactPeopleDict = addressBookDict;
        //联系人分组按拼音分组的Key值
        weakSelf.keys = nameKeys;
        [weakSelf.tableView reloadData];
    } authorizationFailure:^{
//        [weakSelf progressHUDHideImmediately];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"请在iPhone的“设置-隐私-通讯录”选项中，允许访问您的通讯录"
                                                       delegate:nil
                                              cancelButtonTitle:@"知道了"
                                              otherButtonTitles:nil];
        [alert show];
    }];
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    
}
@end
